#include "util.hpp"
#include "data.hpp"
#include "server.hpp"

void FileTest()
{
	vod::FileUtil("./www").CreateDirectory();
	vod::FileUtil("./www/index.html").SetContent("<html></html>");
	std::string body;
	vod::FileUtil("./www/index.html").GetContent(&body);
	std::cout << body << std::endl;
	std::cout << vod::FileUtil("./www/index.html").Size() << std::endl;
}

void JsonTest()
{
	const char *name = "张三";
	int age = 20;
	float score[] = {77.5, 88, 99.5};
	Json::Value val;
	val["姓名"] = name;
	val["年龄"] = age;
	val["成绩"].append(score[0]);
	val["成绩"].append(score[1]);
	val["成绩"].append(score[2]);
	std::string body;
	vod::JsonUtil::Serialize(val, &body);
	std::cout << body.c_str() << std::endl;

	Json::Value stu;
	vod::JsonUtil::UnSerialize(body, &stu);
	std::cout << "姓名: " << stu["姓名"].asString() << std::endl;
	std::cout << "年龄: " << stu["年龄"].asInt() << std::endl;
	std::cout << "成绩: ";
	for (auto &e : stu["成绩"])
	{
		std::cout << e.asFloat() << " ";
	}
	std::cout << std::endl;
}

void DataTest()
{
	vod::TableVideo tb_video;
	// id name info video image
	Json::Value video1;
	video1["name"] = "喜羊羊与灰太狼";
	video1["info"] = "这是一个羊与狼的故事, 很好看!";
	video1["video"] = "./video/sheep_wolf.mp4";
	video1["image"] = "./video/sheep_wolf.jpg";

	Json::Value video2;
	video2["name"] = "变形金刚";
	video2["info"] = "这是一个机器人大战的故事, 很好看!";
	video2["video"] = "./video/reboot.mp4";
	video2["image"] = "./video/reboot.jpg";

	tb_video.Insert(video1);
	tb_video.Insert(video2);

	Json::Value video3;
	tb_video.SelectAll(&video3);
	std::string body;
	vod::JsonUtil::Serialize(video3, &body);
	std::cout << body << std::endl;

	Json::Value video4;
	tb_video.SelectOne(1, &video4);
	vod::JsonUtil::Serialize(video4, &body);
	std::cout << body << std::endl;

	video1["name"] = "喜羊羊与灰太狼第一季";
	video1["info"] = "这是一个羊与狼的故事, 很好看!";
	video1["video"] = "./video/sheep_wolf.mp4";
	video1["image"] = "./video/sheep_wolf.jpg";

	tb_video.Update(1, video1);
	tb_video.Delete(2);

	Json::Value video5;
	tb_video.SelectLike("第一季", &video5);
	vod::JsonUtil::Serialize(video5, &body);
	std::cout << body << std::endl;
}

void ServerTest()
{
	vod::Server server(9090);
	server.RunModule();
}

int main()
{
	// FileTest();
	// JsonTest();
	// DataTest();
	ServerTest();

	return 0;
}